package org.dairy.farms.dao.hibernate;

import org.dairy.farms.dao.LactationDAO;
import org.dairy.farms.dao.generic.AbstractDAOImpl;
import org.dairy.farms.model.Lactation;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jun 13, 2011
 * Time: 3:24:38 PM
 */
@Repository(value = "lactationDao")
public class LactationDAOImpl extends AbstractDAOImpl<Lactation, Long> implements LactationDAO {

    @Override
    public List<Lactation> getLactationDetails(Long livestockId, int page, int start, int limit) {
        Criteria criteria = basePersistence.getSession().createCriteria(Lactation.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id", livestockId)).setFirstResult(start).setMaxResults(limit);
        return criteria.list();
    }

    @Override
    public Long getLactationDetailsCount(Long livestockId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Lactation.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id", livestockId));
        criteria.setProjection(Projections.rowCount());
        return (Long) criteria.uniqueResult();
    }

    @Override
    public List<Lactation> getLactationDetails(Long livestockId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Lactation.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id", livestockId));
        return criteria.list();
    }
}
